/**
 *
 */
package com.ybkj.daijia.server.service.impl;

import com.ybkj.daijia.common.Pager;
import com.ybkj.daijia.common.Principal;
import com.ybkj.daijia.page.Page;
import com.ybkj.daijia.page.Pageable;
import com.ybkj.daijia.page.impl.PageImpl;
import com.ybkj.daijia.page.impl.PageRequest;
import com.ybkj.daijia.server.mapper.OrderMapper;
import com.ybkj.daijia.server.mapper.PhoneVisitMapper;
import com.ybkj.daijia.server.model.Order;
import com.ybkj.daijia.server.model.PhoneVisit;
import com.ybkj.daijia.server.service.PhoneVisitService;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author Shine
 *
 */
@Service
public class DefaultPhoneVisitService implements PhoneVisitService {

    @Autowired
    private PhoneVisitMapper phoneVisitMapper;

    @Autowired
    private OrderMapper orderMapper;

    /*
     * (non-Javadoc)
     *
     * @see com.ybkj.shop.service.AdminService#queryAll()
     */
    public Page<PhoneVisit> queryAll(String param, Date strat, Date end) {

        Subject currentUser = SecurityUtils.getSubject();
        Principal principal = (Principal) currentUser.getPrincipal();

        Pageable pageable = new PageRequest(Pager.getPage(), Pager.getLimit());

        Map<String, Object> params = new HashMap<String, Object>();
        if (!principal.isCompanyType()) {
            params.put("companyId", principal.getCompanyId());
        }
        if (StringUtils.isNotBlank(param)) {
            params.put("name", "%" + param + "%");
        }
        if (null != strat) {
            params.put("strat", strat);
        }
        if (null != end) {
            params.put("end", end);
        }
        params.put("offset", Pager.getOffset());
        params.put("size", Pager.getLimit());

        List<PhoneVisit> content = phoneVisitMapper.selectByParams(params);
        long total = phoneVisitMapper.countByParams(params);

        return new PageImpl<PhoneVisit>(content, pageable, total);
    }

    @Override
    public Page<PhoneVisit> queryByMember(Long memberId, String param) {

        Pageable pageable = new PageRequest(Pager.getPage(), Pager.getLimit());

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("memberId", memberId);
        params.put("offset", Pager.getOffset());
        params.put("size", Pager.getLimit());
        if (StringUtils.isNotBlank(param)) {
            params.put("phone", "%" + param + "%");
        }

        List<PhoneVisit> content = phoneVisitMapper.selectByParams(params);
        long total = phoneVisitMapper.countByParams(params);

        return new PageImpl<PhoneVisit>(content, pageable, total);

    }

    @Override
    public Page<PhoneVisit> pageByMember(final Long memberId, Integer page,
        Integer limit) {

        Pageable pageable = new PageRequest(page, limit);

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("memberId", memberId);
        params.put("offset", page * limit);
        params.put("size", limit);

        List<PhoneVisit> content = phoneVisitMapper.selectByParams(params);
        long total = phoneVisitMapper.countByParams(params);

        return new PageImpl<PhoneVisit>(content, pageable, total);

    }

    @Transactional(rollbackFor = Exception.class)
    public PhoneVisit save(PhoneVisit phoneVisit) {

        phoneVisitMapper.insertRecord(phoneVisit);
        return phoneVisit;
    }

    @Transactional(rollbackFor = Exception.class)
    public PhoneVisit save(PhoneVisit phoneVisit, Order order) {

        order.setHasVisited(true);
        orderMapper.updateByPrimaryKey(order);

        phoneVisitMapper.insertRecord(phoneVisit);
        return phoneVisit;
    }

    public PhoneVisit findOne(Long id) {

        return phoneVisitMapper.selectByPrimaryKey(id);
    }

    @Transactional(rollbackFor = Exception.class)
    public void delete(PhoneVisit phoneVisit) {

        phoneVisitMapper.deleteByPrimaryKey(phoneVisit.getId());

    }

    @Override
    public Page<PhoneVisit> queryAllByParams(String hfName, String khName,
        Date start, Date end) {
        Subject currentUser = SecurityUtils.getSubject();
        Principal principal = (Principal) currentUser.getPrincipal();

        Pageable pageable = new PageRequest(Pager.getPage(), Pager.getLimit());

        Map<String, Object> params = new HashMap<String, Object>();
        if (StringUtils.isNotBlank(hfName)) {
            params.put("hfName", "%" + hfName + "%");
        }
        if (StringUtils.isNotBlank(khName)) {
            params.put("khName", "%" + khName + "%");
        }
        if (start != null) {
            params.put("strat", start);
        }
        if (end != null) {
            params.put("end", end);
        }
        params.put("offset", Pager.getOffset());
        params.put("size", Pager.getLimit());

        List<PhoneVisit> content = phoneVisitMapper.selectByAllParams(params);
        long total = phoneVisitMapper.countByAllParams(params);
        return new PageImpl<PhoneVisit>(content, pageable, total);
    }

}